package org.dromara.demo.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.demo.domain.bo.JsFileBo;
import org.dromara.demo.domain.vo.JsFileVo;
import org.dromara.demo.domain.JsFile;
import org.dromara.demo.mapper.JsFileMapper;
import org.dromara.demo.service.IJsFileService;

import java.util.List;
import java.util.Collection;

/**
 * fileService业务层处理
 *
 * @author zhu
 * @date 2025-01-02
 */
@RequiredArgsConstructor
@Service
public class JsFileServiceImpl implements IJsFileService {

    private final JsFileMapper baseMapper;

    /**
     * 查询file
     *
     * @param id 主键
     * @return file
     */
    @Override
    public JsFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询file列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return file分页列表
     */
    @Override
    public TableDataInfo<JsFileVo> queryPageList(JsFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JsFile> lqw = buildQueryWrapper(bo);
        Page<JsFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的file列表
     *
     * @param bo 查询条件
     * @return file列表
     */
    @Override
    public List<JsFileVo> queryList(JsFileBo bo) {
        LambdaQueryWrapper<JsFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JsFile> buildQueryWrapper(JsFileBo bo) {
        LambdaQueryWrapper<JsFile> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), JsFile::getFileName, bo.getFileName());
        lqw.eq(bo.getExecuteId() != null, JsFile::getExecuteId, bo.getExecuteId());
        return lqw;
    }

    /**
     * 新增file
     *
     * @param bo file
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JsFileBo bo) {
        JsFile add = MapstructUtils.convert(bo, JsFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改file
     *
     * @param bo file
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JsFileBo bo) {
        JsFile update = MapstructUtils.convert(bo, JsFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JsFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除file信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public JsFileVo queryByExecuteId(Long executeId, String type) {
        if (executeId != null) {
            LambdaQueryWrapper<JsFile> lqw = Wrappers.lambdaQuery();
            lqw.eq(JsFile::getExecuteId, executeId);
            lqw.eq(StringUtils.isNotBlank(type), JsFile::getType, type);
            lqw.orderByDesc(JsFile::getId);
            List<JsFileVo> jsFileVos = baseMapper.selectVoList(lqw);
            if (jsFileVos != null && jsFileVos.size() > 0) {
                return jsFileVos.get(0);
            }
        }
        return null;
    }
}
